package com.mingben.betplatform.api.baoge3;

import com.mingben.betplatform.util.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 该搜索策略扫描 xlsx  sheet页 , 万 千 百 十 个 , sheet页的最后一行
 * 如果搜索到指定数字 则投注 当前sheet页的位置
 */
public class SearchStrategy {

    private Logger logger = LoggerFactory.getLogger(SearchStrategy.class);

    private int number ;
    private Workbook workbook;
    public SearchStrategy(int number , Workbook workbook){
        this.number = number;
        this.workbook = workbook;
    }

    /**
     * 搜索sheet
     */
    public List<String> searchBetValue(){
        List<String>  searchSheets = Arrays.asList( "万" , "千" , "百" , "十" , "个");
        //存放多个投注编码
        LinkedList<String> sheetNameAndValue = new LinkedList<String>();
        for(String searchName : searchSheets){
            Sheet sheet = workbook.getSheet(searchName);
            //搜索最后一行
           Row lastRow =  sheet.getRow( 209 );

            System.out.println(" Excel information :" + searchName);
           //有效列数据  [3,53]
            for(int start = 3 ; start <= 53 ; start++){

                int actualValue = -1;
                try{
                    actualValue = (int)lastRow.getCell(start ).getNumericCellValue();

                    System.out.print(String.valueOf(actualValue));
                }catch (IllegalStateException illegalState){
                    if(illegalState.getMessage().equals("Cannot get a numeric value from a text cell")){
                        //尝试转文本
                        String text = lastRow.getCell(start).getStringCellValue();
                        if(text.equals(".")){
                            //如果不是数字 执行下一次搜索
                            System.out.print(".");
                            continue;
                        }
                        actualValue = Integer.valueOf(text);
                        //如果文本.忽略
                        logger.info("read excel data error ,  try to change to number , sheetName {} lastRow , column {} value {}" , searchName , start , actualValue);
                    }
                }

                //期望值
                int expectValue = this.number;

                if(actualValue >= expectValue ){
                    sheetNameAndValue.add(searchName + "," + start);
                }

            }

        }


        //找到应该投注编码
        if(CollectionUtils.isEmpty(sheetNameAndValue)){
            return null;
        }
       LinkedList<String> result = new LinkedList<>();
        for(String sheetNameValue : sheetNameAndValue){
            String sheetName = sheetNameValue.split(",")[0];
            int currentColumn = Integer.valueOf(sheetNameValue.split(",")[1]);
            String betValue = null;
            if(sheetName.equals("万")){
                betValue =  getBetValue(sheetName  , currentColumn ) + "||||";
            }else if(sheetName.equals("千")){
                betValue =  "|"+getBetValue(sheetName  , currentColumn ) + "|||";
            }else if(sheetName.equals("百")){
                betValue =  "||"+getBetValue(sheetName  , currentColumn ) + "||";
            }else if(sheetName.equals("十")){
                betValue =  "|||"+getBetValue(sheetName  , currentColumn ) + "|";
            }else if(sheetName.equals("个")){
                betValue =  "||||"+getBetValue(sheetName  , currentColumn ) ;
            }
            result.add(betValue);
        }


        System.out.println("");
        return result;
    }

    /**
     * 给定一个点 算出实际需要投注的数字
     * @param leftPointY
     * @return
     */
    public String getBetValue(String sheetName , int leftPointY ){
        int rightPointY = leftPointY;
        Sheet sheet = workbook.getSheet(sheetName);
        //投注数字列
        Row leftBet = sheet.getRow( 3 );
        //由于合并单元格 所以只能读取一个列的 3  6 5  5 5   5 5 5 5 5  5
        if(leftPointY < 3 + 6 ){
            leftPointY = 3;
        }else if(leftPointY < 3 + 6 + 5 ){
            leftPointY = 3 + 6 ;
        }else if(leftPointY < 3 + 6 + 5 + 5  ){
            leftPointY = 3 + 6  + 5 ;
        }
        else if(leftPointY < 3 + 6 + 5 + 5 + 5  ){
            leftPointY = 3 + 6  + 5  + 5 ;
        }
        else if(leftPointY < 3 + 6 + 5 + 5 + 5 + 5 ){
            leftPointY = 3 + 6  + 5  + 5 + 5;
        }
        else if(leftPointY < 3 + 6 + 5 + 5 + 5 + 5 + 5  ){
            leftPointY = 3 + 6  + 5  + 5 + 5 + 5;
        }
        else if(leftPointY < 3 + 6 + 5 + 5 + 5 + 5 + 5 + 5 ){
            leftPointY =  3 + 6  + 5  + 5 + 5 + 5 + 5;
        }else if(leftPointY < 3 + 6 + 5 + 5 + 5 + 5 + 5 + 5 + 5  ){
            leftPointY = 3 + 6  + 5  + 5 + 5 + 5 + 5 + 5 ;
        }else if(leftPointY < 3 + 6 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 ){
            leftPointY = 3 + 6 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 ;
        }else if(leftPointY < 3 + 6 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5  ){
            leftPointY = 3 + 6 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 ;
        }


        Cell leftBetCell = leftBet.getCell(leftPointY) ;
        String leftBetValue = null;
        try{
            leftBetValue = leftBetCell.getStringCellValue();
        }catch (IllegalStateException illegalState){
            logger.error("read xlsx error   ,  sheetName {} , column {}  ,try change numeric value ," , sheetName , leftPointY);
            leftBetValue =String.valueOf((int)leftBetCell.getNumericCellValue());
            logger.error("change success , value is  {}," , leftBetValue );
        }

        Row rightBetRow = sheet.getRow(8);

        Cell rightBetCell = rightBetRow.getCell(rightPointY) ;

        int rightBetValue = (int)rightBetCell.getNumericCellValue();

        String betValue =  leftBetValue + String.valueOf(rightBetValue);

        String formedValue = "";

        if(betValue.equals("")){
            return "";
        }
        for(int x = 0; x < betValue.toCharArray().length ;x++){
            char ch =  betValue.toCharArray()[x];
            if(x == betValue.toCharArray().length -1){
                formedValue+=ch;
            }else{
                formedValue+=ch + "&";
            }
        }
        return  formedValue;

    }
}
